package com.nova.android.tvlauncher.ui.fragment.atv

import android.os.Bundle
import com.nova.android.tvlauncher.AppCache
import com.nova.android.tvlauncher.R
import com.nova.android.tvlauncher.bean.atv_bean.ChanelData
import com.nova.android.tvlauncher.databinding.FragmentChannelChildAtvBinding
import com.nova.android.tvlauncher.databinding.ItemChannelChildBinding
import com.nova.android.tvlauncher.constants.ConstantsAtv
import com_github_lib_base.base.BaseVMFragment
import com_github_lib_base.base.BaseViewModel
import com_github_lib_base.ext.clickNoRepeat
import com_github_lib_base.ext.rv.setup
import com_github_lib_base.ext.stringValueWithFormat
import sendLiveEventData

class ChannelChildATVFragment : BaseVMFragment<FragmentChannelChildAtvBinding, BaseViewModel>() {

    override fun initView() {

        val packageName = arguments?.getString(ARG_PACKAGE_NAME)
        mBind.tvTitle.text = com.nova.lib_lang.R.string.cuschannel_txt_5.stringValueWithFormat(packageName.toString())
        val dataList = getChannelDataListByPackage(packageName)?:emptyList()
        mBind.rvApps.setup (dataList.toMutableList()){
            addType(ChanelData::class, R.layout.item_channel_child)
            onBind {
                val binding = getBinding<ItemChannelChildBinding>()
                val dto = data as ChanelData
                binding.tvName.text = dto.channelName
                binding.switchWidget.isChecked = dto.isShowChannelData

                itemView.clickNoRepeat {
                    dto.isShowChannelData = !dto.isShowChannelData
                    binding.switchWidget.isChecked = !binding.switchWidget.isChecked
                    updateWithCopy(packageName?:"",dto.channelId?:0,dto.isShowChannelData)
                    sendLiveEventData(
                        ConstantsAtv.UPDATE_HOME_ITEM,
                        Pair(dto.isShowChannelData, dto.channelId)
                    )
                }

            }
        }
    }

    fun getChannelDataListByPackage(packageName: String?): List<ChanelData>? {
        return packageName?.let { pkg ->
            AppCache.channelList
                .find { it.packageName == pkg } // 匹配包名
                ?.channelDataList // 获取对应数据列表
        }
    }

    fun updateWithCopy(packageName: String, targetId: Long, isShow: Boolean) {
        AppCache.channelList = AppCache.channelList.map { channel ->
            if (channel.packageName == packageName) {
                channel.copy(
                    channelDataList = channel.channelDataList.map { data ->
                        if (data.channelId == targetId) data.copy(isShowChannelData = isShow)
                        else data
                    }.toMutableList()
                )
            } else channel
        }.toMutableList()
    }

    companion object {
        private const val ARG_PACKAGE_NAME = "packageName"
        @JvmStatic
        fun newInstance(packageName: String) =
            ChannelChildATVFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PACKAGE_NAME, packageName) // 关键参数存入Bundle
                }
            }
    }

}